﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Businesses
{
    public static class Pending
    {
        public static Adams.Admin.DataTransfer.Businesses.Pending.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.Businesses.Pending.PageLoad();
            data.Businesss = new Shared.Entities.Businesses.BusinessCollection();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT P.BusinessID,P.CategoryID,P.MemberID,P.StatusID,P.Name,P.Street,P.Street2,P.City,P.State,P.Zip,P.Phone,P.Description,P.CreatedOn,P.ExpiresOn,M.FirstName,M.LastName,M.Email,C.Name As Category,T.TypeID AS TypeID, T.Name AS Type
FROM Businesses.Businesses P 
INNER JOIN Businesses.Categories C ON P.CategoryID = C.CategoryID 
INNER JOIN Businesses.Types T ON C.TypeID = T.TypeID 
INNER JOIN Membership.Members M ON P.MemberID = M.MemberID
WHERE P.StatusID = 1
ORDER BY CreatedOn ASC
", CommandType.Text))
            {
                while (dr.Read())
                {
                    var Business = new Shared.Entities.Businesses.Business();
                    Business.ID = dr["BusinessID"].DbInt();
                    Business.Status = (Shared.Entities.Businesses.Status)dr["StatusID"].DbInt();
                    Business.Name = dr["Name"].DbString();
                    Business.Street = dr["Street"].DbString();
                    Business.Street2 = dr["Street2"].DbString();
                    Business.City = dr["City"].DbString();
                    Business.State = dr["State"].DbString();
                    Business.Zip = dr["Zip"].DbString();
                    Business.Phone = dr["Phone"].DbString();
                    Business.Description = dr["Description"].DbString();
                    Business.CreatedOn = dr["CreatedOn"].DbDate();
                    Business.ExpiresOn = dr["ExpiresOn"].DbDate();

                    Business.Category = new Shared.Entities.Businesses.Category()
                    {
                        ID = dr["CategoryID"].DbInt(),
                        Name = dr["Type"].DbString(),
                        Type = new Shared.Entities.Businesses.Type()
                        {
                            ID = dr["TypeID"].DbInt(),
                            Name = dr["Type"].DbString()
                        }
                    };

                    Business.Member = new Shared.Entities.Membership.Member()
                    {
                        ID = dr["MemberID"].DbInt(),
                        Email = dr["Email"].DbString(),
                        FirstName = dr["FirstName"].DbString(),
                        LastName = dr["LastName"].DbString(),
                    };

                    data.Businesss.Add(Business);
                }
            }

            return data;
        }

        public static void UpdateStatus(Adams.Admin.DataTransfer.Businesses.Pending.UpdateStatusSave input)
        {
            DB.Run(@"
UPDATE Businesses.Businesses
SET StatusID = @StatusID, ExpiresOn = @ExpiresOn
WHERE BusinessID = @BusinessID
", CommandType.Text,
                    DB.CreateParameter("@BusinessID", input.BusinessID, SqlDbType.Int),
                    DB.CreateParameter("@StatusID", input.StatusID, SqlDbType.Int),
                    DB.CreateParameter("@ExpiresOn", input.ExpiresOn));
        }
    }
}
